package com.zgl.airoleplay.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zgl.airoleplay.entity.Role;
import com.zgl.airoleplay.mapper.RoleMapper;
import com.zgl.airoleplay.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public Result getAllRoles() {
        List<Role> roleList = roleMapper.selectList(null);
        if (roleList == null || roleList.isEmpty()){
            return Result.error("500","获取角色列表失败");
        }
        return Result.success("200", "获取角色列表成功", roleList);
    }

    @Override
    public Result getRoleByName(String roleName) {
        if (roleName == null || roleName.trim().isEmpty()) {
            return getAllRoles();
        } else {
            QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("role_name", roleName.trim());
            Role role = roleMapper.selectOne(queryWrapper);
            if (role == null) {
                return Result.error("500", "获取角色失败");
            }
            return Result.success("200", "获取角色成功", role);
        }
    }
}
